import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 数据持久化类，负责成绩数据的保存和加载
 */
public class DataPersistence {
    private static final String DATA_FILE = "grades.dat";
    private static final Logger LOGGER = Logger.getLogger(DataPersistence.class.getName());

    public static void saveGrades(List<Grade> grades) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
            oos.writeObject(grades);
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "保存成绩数据失败", e);
        }
    }

    @SuppressWarnings("unchecked")
    public static List<Grade> loadGrades() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
            return (List<Grade>) ois.readObject();
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException | ClassNotFoundException e) {
            LOGGER.log(Level.SEVERE, "加载成绩数据失败", e);
            return null;
        }
    }
}    